﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Budget
{
    public class DataStuff
    {
        public static List<DateTime> FridaysInMonth;
        public static Dictionary<DateTime, decimal> TotalsForWeekIncoming = new Dictionary<DateTime, decimal>();
        public static Dictionary<DateTime, decimal> TotalsForWeekOutGoing = new Dictionary<DateTime, decimal>();
        public static Dictionary<DateTime, decimal> TotalsForWeekOutGoingFixed = new Dictionary<DateTime, decimal>();
        public static Dictionary<DateTime, decimal> TotalsForWeek = new Dictionary<DateTime, decimal>();
        public static Dictionary<string, decimal> CategoryTotals = new Dictionary<string, decimal>();
        

        public DataStuff(int month, int year)
        {
            FridaysInMonth = Dates.getFridaysInMonthList(month, year);
            TotalsForWeek.Clear();
            TotalsForWeekIncoming.Clear();
            TotalsForWeekOutGoing.Clear();
            TotalsForWeekOutGoingFixed.Clear();

            foreach (DateTime d in DataStuff.FridaysInMonth)
            {
                TotalsForWeek.Add(d, 0);
                TotalsForWeekIncoming.Add(d, 0);
                TotalsForWeekOutGoing.Add(d, 0);
                TotalsForWeekOutGoingFixed.Add(d, 0);
            }
            Dictionary<string, decimal> temp = new Dictionary<string, decimal>();
            foreach (string s in dbData.listExpenseCategories)
            {
                temp.Add(s,0);
            }
            foreach (string s in dbData.listIncomeCategories)
            {
                temp.Add(s, 0);
            }
            if (!temp.ContainsKey("Incoming"))
            {
                temp.Add("Incoming", 0);
            }
            if (!temp.ContainsKey("Expenses"))
            {
                temp.Add("Expenses", 0);
            }
            if (!temp.ContainsKey("Net"))
            {
                temp.Add("Net", 0);
            }
            CategoryTotals = temp;
        }
    }
    public class Money
    {
        public static string MoneyFormat = "#0.00";
        public Dictionary<string, decimal> dict_WeekTotalForCategory;
        public decimal TotalIncome;
        public decimal TotalFixedExpenses;
        public decimal TransactionAmount;
        public DateTime TransactionDate;
        public DataTable dt;

        public Money()
        {
            InitializeStructs();
        }
        public Money(int month)
        {
            InitializeStructs();
        }
        public void InitializeStructs()
        {
            dict_WeekTotalForCategory = new Dictionary<string, decimal>();
        }
        public enum TransactionType {Income, Expense}; 
    }
    class Income : Money
    {
        int Month;
        public List<Dictionary<string, decimal>> list_TransactionsByCategoryForWeek;
        public Income()
        {

        }
        public Income(int month)
        {
            this.Month = month;
            list_TransactionsByCategoryForWeek = new List<Dictionary<string, decimal>>();
            foreach (DateTime d in DataStuff.FridaysInMonth)
            {
                getIncomeForWeek(d);
            }
        }
        public void getIncomeForWeek(DateTime date)
        {
            dt = DatabaseQuery.ExecStringProc(StoredProcs.getIncomeForWeek((date.AddDays(-6)).ToString("MM/dd/yyyy"), date.ToString("MM/dd/yyyy")));
            dict_WeekTotalForCategory = new Dictionary<string, decimal>();
            foreach (string s in dbData.listIncomeCategories)
            {
                dict_WeekTotalForCategory.Add(s, 0);
            }
            foreach (DataRow dr in dt.Rows)
            {
                dict_WeekTotalForCategory[dr["tblCategoryIncome.Category"].ToString()] += Convert.ToDecimal(dr["Amount"]);
                DataStuff.TotalsForWeek[date] += Convert.ToDecimal(dr["Amount"]);
                DataStuff.TotalsForWeekIncoming[date] += Convert.ToDecimal(dr["Amount"]);
                DataStuff.CategoryTotals["Incoming"] += Convert.ToDecimal(dr["Amount"]);
                DataStuff.CategoryTotals["Net"] += Convert.ToDecimal(dr["Amount"]);
                DataStuff.CategoryTotals[dr["tblCategoryIncome.Category"].ToString()] += Convert.ToDecimal(dr["Amount"]);
            }
            list_TransactionsByCategoryForWeek.Add(dict_WeekTotalForCategory);
            
        }
    }
    class Expenses : Money
    {
        int Month;
        public List<Dictionary<string, decimal>> list_TransactionsByCategoryForWeek;
        public Expenses()
        {
        }
        public Expenses(int month)
        {
            this.Month = month;
            list_TransactionsByCategoryForWeek = new List<Dictionary<string, decimal>>();
            foreach (DateTime d in DataStuff.FridaysInMonth)
            {
                GetExpensesForWeek(d);
            }
        }

        public void GetExpensesForWeek(DateTime date)
        {
            dt = DatabaseQuery.ExecStringProc( StoredProcs.getExpenseForWeek( (date.AddDays(-6)).ToString("MM/dd/yyyy"),date.ToString("MM/dd/yyyy")));
            dict_WeekTotalForCategory = new Dictionary<string, decimal>();
            foreach (string s in dbData.listExpenseCategories)
            {
                dict_WeekTotalForCategory.Add(s, 0);
            }
            foreach (DataRow dr in dt.Rows)
            {
                dict_WeekTotalForCategory[dr["tblCategoryExpense.Category"].ToString()] += Convert.ToDecimal( dr["Amount"]);
                DataStuff.TotalsForWeek[date] -= Convert.ToDecimal(dr["Amount"]);
                DataStuff.TotalsForWeekOutGoing[date] += Convert.ToDecimal(dr["Amount"]);
                DataStuff.CategoryTotals["Net"] -= Convert.ToDecimal(dr["Amount"]);
                DataStuff.CategoryTotals["Expenses"] += Convert.ToDecimal(dr["Amount"]);
                DataStuff.CategoryTotals[dr["tblCategoryExpense.Category"].ToString()] += Convert.ToDecimal(dr["Amount"]);
                
                if(dr["Fixed"].ToString() == "1")
                {
                    DataStuff.TotalsForWeekOutGoingFixed[date] += Convert.ToDecimal(dr["Amount"]);
                }

                
            }
            list_TransactionsByCategoryForWeek.Add(dict_WeekTotalForCategory);
        }
    }
}